package com.mjk.datasource;


import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.base.work.BaseWork;
import com.mjk.common.base.work.annotation.BeeWork;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;

import com.mjk.common.util.SnowFlake;
import com.mjk.datasource.model.CustomDatasourceModel;
import org.springframework.beans.factory.annotation.Autowired;

@BeeWork(work = "customdatasource")
public class CustomDatasourceWork extends BaseWork {
    @Autowired
    private BeeDBService beeDBService;

    public BeeResponse getCustomDatasource(CustomDatasourceModel model){
        needParams(model,CustomDatasourceModel::getGroupKey);
        BeeQueryWrapper wrapper = new BeeQueryWrapper(model);
        wrapper.eq(CustomDatasourceModel::getGroupKey,model.getGroupKey());
        return BeeError.BEE_SUC.tranceError(beeDBService.findManyByWrapper(wrapper));
    }

    public BeeResponse insertCustomDatasource(CustomDatasourceModel model){
        needParams(model,CustomDatasourceModel::getGroupKey);
        model.setId(SnowFlake.getUid());
        beeDBService.save(model);
        return BeeError.BEE_SUC.tranceError();
    }

    public BeeResponse updateCustomDatasource(CustomDatasourceModel model){
        needParams(model,CustomDatasourceModel::getId);

        BeeQueryWrapper wrapper = new BeeQueryWrapper(model);
        wrapper.eq(CustomDatasourceModel::getId,model.getId());
        beeDBService.update(wrapper);
        return BeeError.BEE_SUC.tranceError();
    }

    public BeeResponse delCustomDatasource(CustomDatasourceModel model){
        needParams(model,CustomDatasourceModel::getId);
        BeeQueryWrapper wrapper = new BeeQueryWrapper(model);
        wrapper.eq(CustomDatasourceModel::getId,model.getId());
        beeDBService.delete(wrapper);
        return BeeError.BEE_SUC.tranceError();
    }
}
